package com.tbit.uqbike.convert;

import com.tbit.uqbike.object.pojo.TransactionAbnormal;
import com.tbit.uqbike.webmanager.dto.AbnormalRecordDTO;
import com.tbit.uqbike.webmanager.resp.reconciliation.HandleDetailResp;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * BadOrder -> AbnormalRecordDTO
 *
 *
 * @author <a href="mailto:chen.chaoxiong@tbit.com.cn">ChenChaoXiong</a>
 * @since 2024/5/21
 */
public class BadOrderMapper {



    public static List<AbnormalRecordDTO> poToDTO(List<TransactionAbnormal> badOrderList) {
        if ( badOrderList == null ) {
            return null;
        }

        List<AbnormalRecordDTO> list = new ArrayList<AbnormalRecordDTO>( badOrderList.size() );
        for ( TransactionAbnormal transactionAbnormal : badOrderList ) {
            list.add( poToDTO( transactionAbnormal ) );
        }

        return list;
    }

    public static AbnormalRecordDTO poToDTO(TransactionAbnormal transactionAbnormal)  {
        if ( transactionAbnormal == null ) {
            return null;
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sourceTradeTime=null;
        Date targetTradeTime=null;
        try {
            if (Objects.nonNull(transactionAbnormal.getSourceTradeTime())){
                sourceTradeTime = formatter.parse(transactionAbnormal.getSourceTradeTime());
            }
            if (Objects.nonNull(transactionAbnormal.getTargetTradeTime())){
                targetTradeTime = formatter.parse(transactionAbnormal.getTargetTradeTime());
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        AbnormalRecordDTO abnormalRecordDTO = new AbnormalRecordDTO();

        abnormalRecordDTO.setRecordId( transactionAbnormal.getId().toString() );
        abnormalRecordDTO.setSourceTradeTime( sourceTradeTime );
        abnormalRecordDTO.setTargetTradeTime( targetTradeTime );
        abnormalRecordDTO.setSourceAmount( transactionAbnormal.getSourceAmount() );
        abnormalRecordDTO.setTargetAmount( transactionAbnormal.getTargetAmount() );
        abnormalRecordDTO.setSourceTradeStatus( transactionAbnormal.getSourceTradeStatus().getStatus() );
        abnormalRecordDTO.setTargetTradeStatus( transactionAbnormal.getTargetTradeStatus().getStatus() );
        abnormalRecordDTO.setAccountId( transactionAbnormal.getAccountId() );
        abnormalRecordDTO.setAccountName( transactionAbnormal.getAccountName() );
        abnormalRecordDTO.setChannel( transactionAbnormal.getChannel() );
        abnormalRecordDTO.setUnbalanceType( transactionAbnormal.getUnbalanceType() );
        abnormalRecordDTO.setUserName( transactionAbnormal.getUserName() );
        abnormalRecordDTO.setUserPhone( transactionAbnormal.getUserPhone() );
        abnormalRecordDTO.setOutOrderNo( transactionAbnormal.getOutOrderNo() );
        abnormalRecordDTO.setOrdrNo( transactionAbnormal.getOrdrNo() );
        abnormalRecordDTO.setRemark( transactionAbnormal.getRemark() );

        handleTypeToProcessStauts( transactionAbnormal, abnormalRecordDTO );

        return abnormalRecordDTO;
    }

    public static HandleDetailResp poToHandleDetailResp(TransactionAbnormal transactionAbnormal) {
        if ( transactionAbnormal == null ) {
            return null;
        }

        HandleDetailResp handleDetailResp = new HandleDetailResp();

        handleDetailResp.setRecordId( transactionAbnormal.getId().toString() );
        handleDetailResp.setProcessTime( transactionAbnormal.getOperatorTime() );
        handleDetailResp.setSourceAmount( transactionAbnormal.getSourceAmount() );
        handleDetailResp.setTargetAmount( transactionAbnormal.getTargetAmount() );
        handleDetailResp.setRefundAmount(transactionAbnormal.getSourceAmount());
        handleDetailResp.setOprator( transactionAbnormal.getOperatorName() );
        handleDetailResp.setHandleType( transactionAbnormal.getHandleMethod() );
        handleDetailResp.setRemark( transactionAbnormal.getRemark() );

        handleTypeToProcessStauts( transactionAbnormal, handleDetailResp );

        return handleDetailResp;
    }


    public static void handleTypeToProcessStauts(TransactionAbnormal transactionAbnormal,  AbnormalRecordDTO abnormalRecordDTO) {
        if (Objects.nonNull(transactionAbnormal) && transactionAbnormal.getHandleType() != null) {
            abnormalRecordDTO.setProcessStauts(transactionAbnormal.getHandleType().getType());
        }
        if (transactionAbnormal.getBusinessType() != null) {
            abnormalRecordDTO.setFlowType(transactionAbnormal.getBusinessType().getType());
        }
        if (Objects.nonNull(transactionAbnormal.getHandleType()) && transactionAbnormal.getHandleType().getType().equals("processed")){
            abnormalRecordDTO.setOprator(transactionAbnormal.getOperatorName());
            abnormalRecordDTO.setOpratorPhone(transactionAbnormal.getOperatorPhone());
            abnormalRecordDTO.setProcessTime(transactionAbnormal.getOperatorTime());
        }
    }

    public static void handleTypeToProcessStauts(TransactionAbnormal transactionAbnormal,  HandleDetailResp handleDetailResp) {
        if (Objects.nonNull(transactionAbnormal) && transactionAbnormal.getHandleType() != null) {
            handleDetailResp.setProcessStauts(transactionAbnormal.getHandleType().getType());
            if (transactionAbnormal.getHandleType().getType().equals("processed")) {
                handleDetailResp.setRefundAmount(Objects.nonNull(transactionAbnormal.getSourceAmount())?transactionAbnormal.getSourceAmount():transactionAbnormal.getTargetAmount());
                handleDetailResp.setProcessTime( transactionAbnormal.getOperatorTime() );
            }
        }
    }

}
